package com.taobao.askwho.db.impl;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.taobao.askwho.db.base.MysqlRouteBase;
import com.taobao.askwho.po.PersonInfo;
import com.taobao.askwho.po.UrlInfo;
import com.taobao.askwho.utils.DateUtil;
import com.taobao.askwho.vo.PersonalPageRecordVo;

public class CombineDao extends MysqlRouteBase {
	public Map<String,PersonalPageRecordVo> findUrlTagRecordByUserUuid(String uuid) throws Exception {
		String sql = "SELECT rel.*,u.`url_content` AS content,u.`createtime`,u.`updatetime` FROM tag_relation rel, url_info u WHERE rel.`related_uuid` = u.`uuid` and rel.`user_uuid` = ?";
		//相同内容的数据合并为一起
		final Map<String, PersonalPageRecordVo> map = new HashMap<String, PersonalPageRecordVo>();
		
		this.query(sql, new Object[]{uuid}, new SqlCallBack() {
			@Override
			public void readerRows(ResultSet rs) throws Exception {
				String content = rs.getString("content");
				PersonalPageRecordVo vo = map.get(content);
				if(vo == null) {
					vo = new PersonalPageRecordVo("url");
					map.put(content, vo);
				}
				final UrlInfo info = vo.getUrlInfo();
				info.setUrlContent(content);
				String tagUuidsString = vo.getTagUuidsString();
				if(tagUuidsString == null || tagUuidsString.trim().equals("")) {
					tagUuidsString = rs.getString("uuid");
				} else {
					tagUuidsString += "," + rs.getString("uuid");
				}
				vo.setTagUuidsString(tagUuidsString);
				vo.getTagNamesSet().add(rs.getString("tagname"));
				vo.setUserUuid(rs.getString("user_uuid"));
				vo.setPublic(rs.getBoolean("ispublic"));
				vo.setCreateTimeStr(DateUtil.cutLastNumOfSqlTimeString(rs.getString("createtime")));
				vo.setUpdateTimeStr(DateUtil.cutLastNumOfSqlTimeString(rs.getString("updatetime")));	
			}
			});
		return map;
	}
	
	public Map<String,PersonalPageRecordVo> findPeopleTagRecordByUserUuid(String uuid) throws Exception {
		String sql = "SELECT rel.*,u.`person_name` AS person_name,u.`wangwang`,u.`md5`,u.`createtime`,u.`updatetime` FROM tag_relation rel, person_info u  where rel.`related_uuid` = u.`uuid` and rel.`user_uuid` = ?";
		//相同内容的数据合并为一起
		final Map<String, PersonalPageRecordVo> map = new HashMap<String, PersonalPageRecordVo>();
		
		this.query(sql, new Object[]{uuid}, new SqlCallBack() {
			@Override
			public void readerRows(ResultSet rs) throws Exception {
				String md5 = rs.getString("md5");	//通过md5快速判断记录是否相同
				PersonalPageRecordVo vo = map.get(md5);
				if(vo == null) {
					vo = new PersonalPageRecordVo("person");
					map.put(md5, vo);
				}
				final PersonInfo info = vo.getPersonInfo();
				info.setMd5(md5);
				info.setWangwang(rs.getString("wangwang"));
				info.setPersonName(rs.getString("person_name"));
				String tagUuidsString = vo.getTagUuidsString();
				if(tagUuidsString == null || tagUuidsString.trim().equals("")) {
					tagUuidsString = rs.getString("uuid");
				} else {
					tagUuidsString += "," + rs.getString("uuid");
				}
				vo.setTagUuidsString(tagUuidsString);
				vo.getTagNamesSet().add(rs.getString("tagname"));
				vo.setUserUuid(rs.getString("user_uuid"));
				vo.setPublic(rs.getBoolean("ispublic"));
				vo.setCreateTimeStr(DateUtil.cutLastNumOfSqlTimeString(rs.getString("createtime")));
				vo.setUpdateTimeStr(DateUtil.cutLastNumOfSqlTimeString(rs.getString("updatetime")));	
			}
			});
		return map;
	}	
	
	//根据uuids查询
	public Map<String, PersonalPageRecordVo> findResultByUuids(List<String> uuidList) throws Exception {
		//暂时做两次查询,把结果做合并
		String inStr = "";
		for(String uuid: uuidList) {
			inStr += "'" + uuid + "',";
		}
		if(uuidList.size() > 0)
			inStr = inStr.substring(0,inStr.length() - 1);
		else
			return new HashMap<String, PersonalPageRecordVo>();
		
		String sqlUrl = "SELECT rel.*,u.`url_content` AS content,u.`createtime`,u.`updatetime` FROM tag_relation rel, url_info u WHERE rel.`related_uuid` = u.`uuid` AND rel.`uuid` IN (" + inStr + ")";
		//related_uuid, po
		final Map<String, PersonalPageRecordVo> map = new HashMap<String, PersonalPageRecordVo>();
		
		this.query(sqlUrl, new SqlCallBack() {
			@Override
			public void readerRows(ResultSet rs) throws Exception {
				PersonalPageRecordVo vo = map.get(rs.getString("related_uuid"));
				if(vo == null) {
					vo = new PersonalPageRecordVo("url");
					map.put(rs.getString("uuid"), vo);
				}
				final UrlInfo info = vo.getUrlInfo();
				info.setUrlContent(rs.getString("content"));
				info.setUuid(rs.getString("related_uuid"));
				vo.setTagUuidsString(rs.getString("uuid"));
				vo.getTagNamesSet().add(rs.getString("tagname"));
				vo.setUserUuid(rs.getString("user_uuid"));
				vo.setPublic(rs.getBoolean("ispublic"));
				vo.setCreateTimeStr(DateUtil.cutLastNumOfSqlTimeString(rs.getString("createtime")));
				vo.setUpdateTimeStr(DateUtil.cutLastNumOfSqlTimeString(rs.getString("updatetime")));	
			}
			});
		String sqlPerson = "SELECT rel.*,u.`person_name` AS person_name,u.`wangwang`,u.`md5`,u.`createtime`,u.`updatetime` FROM tag_relation rel, person_info u  where rel.`related_uuid` = u.`uuid` AND rel.`uuid` IN (" + inStr + ")";
		this.query(sqlPerson, new SqlCallBack() {
			@Override
			public void readerRows(ResultSet rs) throws Exception {
				PersonalPageRecordVo vo = map.get(rs.getString("related_uuid"));
				if(vo == null) {
					vo = new PersonalPageRecordVo("person");
					map.put(rs.getString("uuid"), vo);
				}
				final PersonInfo info = vo.getPersonInfo();
				info.setMd5(rs.getString("md5"));
				info.setWangwang(rs.getString("wangwang"));
				info.setPersonName(rs.getString("person_name"));
				info.setUuid(rs.getString("related_uuid"));
				vo.setTagUuidsString(rs.getString("uuid"));
				vo.getTagNamesSet().add(rs.getString("tagname"));
				vo.setUserUuid(rs.getString("user_uuid"));
				vo.setPublic(rs.getBoolean("ispublic"));
				vo.setCreateTimeStr(DateUtil.cutLastNumOfSqlTimeString(rs.getString("createtime")));
				vo.setUpdateTimeStr(DateUtil.cutLastNumOfSqlTimeString(rs.getString("updatetime")));	
			}
			});		
		return map;
	}
}
